function EventEmitter() {
    this.events = {};
    this._maxListeners = 10;
    this.showError = true;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener = function (type, listener) {
    if (this.events[type]) {
        this.events[type].push(listener);
    } else {
        this.events[type] = [listener];
    }
}
EventEmitter.prototype.once = function (type, listener) {
    let wrap = (...ags) => {
        listener.apply(this,ags);
        this.removLister(type,wrap)
    };
    this.on(type,wrap);
}
EventEmitter.prototype.emit = function (type, ...ags) {
    if(this.events[type]) {
        this.events[type].forEach(listener => listener.apply(this, ags));
        if(this.events[type].length > this._maxListeners && this._maxListeners != 0 && this.showError) {
            this.showError = false;
            console.error(`MaxListenersExceededWarning: Possible EventEmitter memory leak detected. ${this._maxListeners + 1} ${type} listeners added. Use emitter.setMaxListeners() to increase limit`)
        }
    }
}
EventEmitter.prototype.removLister = function(type, listener) {
    if(this.events[type]) {
        this.events[type] = this.events[type].filter(lis => lis != listener)
    }
}
EventEmitter.prototype.removAllLister = function(type) {   
    delete this.events[type]; 
}
EventEmitter.prototype.listeners = function(type) {   
    return this.events[type];
}
EventEmitter.prototype.setMaxListeners = function(num) {   
    this._maxListeners = num;
}
module.exports = EventEmitter;